﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnumHelper = QunProject.Model.EnumHelper;

namespace QunProject.Manage.Controllers
{
    public class ComplainController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 获取数据
        public JsonResult AjaxData(int page = 1, int limit = 20)
        {
            var list = EF.complain.Where(x => true);

            var data = list.OrderByDescending(x => x.CreateTime).ToList().Skip((page - 1) * limit).Take(limit).Select(x => new
            {
                x.ID,
                x.Remark,
                x.ObjectType,
                ObjectTypeName = Enum.GetName(typeof(EnumHelper.ComplainType), x.ObjectType),
                x.ObjectID,
                x.Content,
                x.Processing,
                ProcessingName = Enum.GetName(typeof(EnumHelper.Processing), x.Processing),
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
            });

            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 提交处理
        public JsonResult AjaxProcessing(int complainId, int processing, string remark = "")
        {
            var complain = EF.complain.SingleOrDefault(x => x.ID == complainId);
            if (complain == null)
                return Json(Result.Fail("举报错误"));

            complain = EF.complain.Attach(complain);
            complain.Processing = processing;
            complain.Remark = remark;
            if (complain.ObjectType == (int)EnumHelper.ComplainType.文章)
            {
                if (processing == (int)EnumHelper.Processing.删除)
                {
                    var article = EF.article.SingleOrDefault(x => x.ID == complain.ObjectID);
                    article = EF.article.Attach(article);
                    article.IsValid = false;
                }
                bool isSucc = EF.SaveChanges() > 0;
                return isSucc ? Json(Result.Success()) : Json(Result.Fail());
            }
            else if (complain.ObjectType == (int)EnumHelper.ComplainType.评论)
            {
                if (processing == (int)EnumHelper.Processing.删除)
                {
                    var reply = EF.reply.SingleOrDefault(x => x.ID == complain.ObjectID);
                    reply = EF.reply.Attach(reply);
                    reply.IsDisplay = false;
                }
                bool isSucc = EF.SaveChanges() > 0;
                return isSucc ? Json(Result.Success()) : Json(Result.Fail());
            }
            else
            {
                return Json(Result.Fail("举报类型错误"));
            }
        }
    }
}